package org.contextinferenceengine;

import java.util.Calendar;
import java.util.List;

import org.contextinferenceengine.location.LocationContainer;
import org.contextinferenceengine.location.LocationTrackingService;

import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;



public class DataViewActivity extends ListActivity {

	private Button startButton;
	private Button refreshButton;

	private DatabaseHelper dbHelper;
	private TextView header;

	
	// private ListView list;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		dbHelper = new DatabaseHelper(this);

		this.setContentView(R.layout.main);

		this.refreshButton = (Button) findViewById(R.id.refresh_button);
		this.startButton = (Button) findViewById(R.id.start_button);
		this.header = (TextView) findViewById(R.id.textview);
		
		
		// this.list = (ListView) findViewById(android.R.id.list);
		// this.list = listAdapter,

		getListView().setAdapter(
				new LocationAdapter(this, android.R.layout.simple_list_item_1,
						dbHelper.selectAll()));

		this.refreshButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				getListView().setAdapter(
						new LocationAdapter(DataViewActivity.this,
								android.R.layout.simple_list_item_1, dbHelper
										.selectAll()));

			}
		});

		this.startButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (LocationTrackingService.isAnInstanceRunning()) {
					stopService(new Intent(DataViewActivity.this,
							LocationTrackingService.class));

				} else {
					startService(new Intent(DataViewActivity.this,
							LocationTrackingService.class));
				}

			}
		});

		startHeaderUpdater();
	}

	private void startHeaderUpdater() {

		final Handler uiThreadHandler = new Handler();

		new Thread(new Runnable() {

			@Override
			public void run() {

				while (true) {

					try {
						Thread.sleep(5000);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}

					if (LocationTrackingService.isAnInstanceRunning()) {
						uiThreadHandler.post(new Runnable() {

							@Override
							public void run() {
								header.setText("location servce is running");
								startButton.setText("Stop service");
							}
						});
					} else {
						uiThreadHandler.post(new Runnable() {

							@Override
							public void run() {
								header.setText("location servce not running");
								startButton.setText("Start service");
							}
						});
					}
				}
			}
		}).start();

	}

	private class LocationAdapter extends ArrayAdapter<LocationContainer> {

		private List<LocationContainer> items;

		public LocationAdapter(Context context, int textViewResourceId,
				List<LocationContainer> objects) {
			super(context, textViewResourceId, objects);
			this.items = objects;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			View v = convertView;
			if (v == null) {
				LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
				v = vi.inflate(R.layout.row, null);
			}
			LocationContainer container = items.get(position);
			Location o = container.getLocation();
			if (o != null) {

				TextView time = (TextView) v.findViewById(R.id.timedate);
				
				
				TextView latlonalt = (TextView) v.findViewById(R.id.latlonalt);
				ProgressBar accuracy = (ProgressBar) v.findViewById(R.id.accurasy);
				ImageView provider = (ImageView) v.findViewById(R.id.provider);

				Calendar c = Calendar.getInstance();
				c.setTimeInMillis(o.getTime());
				
				int year = c.get(Calendar.YEAR);
				int month = c.get(Calendar.MONTH) + 1;
				int day = c.get(Calendar.DAY_OF_MONTH);
								
				int hour = c.get(Calendar.HOUR_OF_DAY);
				int minute = c.get(Calendar.MINUTE);

				time.setText("" + year + "-"+month+"-"+day+" "+hour+":"+minute );
				
				String addressline = "lat: " + o.getLatitude() + " lon: " + o.getLongitude() + " alt: " + o.getAltitude();
				
				addressline = container.getAddress();
				
				latlonalt.setText(addressline);

				accuracy.setProgress((int)o.getAccuracy());
				
				Log.d("provider", "provider: "+o.getProvider());
				if("network".equals(o.getProvider())){
					provider.setImageResource(R.drawable.network_icon);		
					
				}else if("gps".equals(o.getProvider())){
					provider.setImageResource(R.drawable.gps_icon);
				}else{
					provider.setImageResource(R.drawable.under_construction);
				}

			}
			return v;
		}
	}

}
